﻿Imports System.Windows.Forms
Imports ArfaUI
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class MasterSupplierPage
    Inherits PageTemplate
    Dim vParamsField As String()
    Dim vFieldControl As Control
    Dim DGV As New DataGridView
    Dim vProcExecute As String


    Sub setbutton(ByVal execname As String, ByVal active As Boolean)
        Dim btnname() As String
        btnname = New String() {"Add", "Edit", "Delete"}

        For Each ctl As Control In PnlBtn.Controls
            If TypeOf ctl Is Button Then
                If ctl.Tag = btnname(0) Or ctl.Tag = btnname(1) Or ctl.Tag = btnname(2) Then
                    If active Then
                        ctl.Enabled = False
                    Else
                        ctl.Enabled = True
                    End If
                    If ctl.Tag = execname Then
                        If active Then
                            ctl.Enabled = True
                            ctl.Text = "Save"
                        Else
                            ctl.Text = ctl.Tag
                        End If
                    Else
                        ctl.Text = ctl.Tag
                    End If
                End If
            End If
        Next

    End Sub

    Function validationAdd() As Boolean
        Dim retval As Boolean = False
        If Not vidsupplier.Text = String.Empty And Not vnama.Text = String.Empty Then
            retval = True
        End If
        Return retval
    End Function
    Function validationEdit() As Boolean
        Dim retval As Boolean = False
        If Not vidsupplier.Text = String.Empty And Not vnama.Text = String.Empty Then
            retval = True
        End If
        Return retval
    End Function
    Function validationDel() As Boolean
        Dim retval As Boolean = False
        If Not vidsupplier.Text = String.Empty And Not vnama.Text = String.Empty Then
            retval = True
        End If
        Return retval
    End Function
    Dim ParamFinder() As Object
    Sub ParamFind()
        ParamFinder = New Object() {"*", "rssupplier", "nama", ""}
    End Sub
    Sub searchitem()
        Dim dtitem As New DataTable
        If Utils.executeSP("proc_searchall", ParamFinder, dtitem) Then
            If dtitem IsNot Nothing Then
                DGV.DataSource = dtitem
            End If
        End If
    End Sub
    Sub RefreshValue()
        For Each ctl As Control In vFieldControl.Controls
            If TypeOf ctl Is TextBox Then
                If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                    Dim cnum As AMS.TextBox.NumericTextBox
                    cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                    cnum.Text = 0
                Else
                    ctl.Text = ""
                End If
                ctl.Enabled = False
            ElseIf TypeOf ctl Is ComboBox Then
                Dim cb As ComboBox
                cb = CType(ctl, ComboBox)
                cb.Enabled = False
            ElseIf TypeOf ctl Is CheckBox Then
                Dim cbx As CheckBox
                cbx = CType(ctl, CheckBox)
                cbx.Checked = False
                cbx.Enabled = False
            End If
        Next
        ParamFind()
        searchitem()
        setbutton("", False)
        DGV.Enabled = True

    End Sub
    Function setparamitem(ByVal exec As String) As Object()
        Dim param As New List(Of Object)

        param.Clear()
        param.Add(exec)
        For Each s As String In vParamsField
            For Each ctl As Control In vFieldControl.Controls
                If ctl.Name = s Then

                    If TypeOf ctl Is TextBox Then
                        If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                            Dim cnum As AMS.TextBox.NumericTextBox
                            cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                            If cnum.Text = "" Then cnum.Text = 0
                            param.Add(CDbl(cnum.NumericText))
                        Else
                            Dim ctlline As TextBox
                            ctlline = CType(ctl, TextBox)
                            If ctlline.Multiline Then
                                If ctlline.Lines.Length > 0 Then
                                    'Dim bf As New BinaryFormatter
                                    'Dim ms As New MemoryStream
                                    'Dim bytearr As Byte()
                                    'ms = New MemoryStream
                                    'bf.Serialize(ms, ctlline.Lines)
                                    'bytearr = ms.ToArray
                                    Dim vmemo As String = String.Empty
                                    Dim strarr As String() = RTrim(ctlline.Text).Split(vbCrLf)

                                    For Each st As String In strarr
                                        If Not st = vbCrLf Then
                                            vmemo &= LTrim(RTrim(st))
                                            vmemo &= "\n"
                                        End If
                                    Next
                                    'For i As Integer = 0 To ctlline.Lines.Length - 1
                                    '    vmemo += ctlline.Lines(i).ToString
                                    '    If i < ctlline.Lines.Length - 1 Then
                                    '        vmemo += "\n"
                                    '    End If
                                    'Next
                                    param.Add(vmemo)
                                Else
                                    param.Add(Convert.DBNull)
                                End If
                            Else
                                param.Add(IIf(ctl.Text = "", Convert.DBNull, ctl.Text))
                            End If

                        End If
                    ElseIf TypeOf ctl Is ComboBox Then
                        Dim cbx As ComboBox
                        cbx = CType(ctl, ComboBox)
                        If cbx.DataSource IsNot Nothing Then
                            param.Add(cbx.SelectedValue)
                        Else
                            param.Add(cbx.Text)
                        End If
                    ElseIf TypeOf ctl Is CheckBox Then
                        Dim cbx As CheckBox
                        cbx = CType(ctl, CheckBox)

                        param.Add(IIf(cbx.Checked, 1, 0))
                    End If
                End If
            Next
        Next
        Return param.ToArray
    End Function

    Sub ExecItem(ByVal execname As String)
        Dim dt As New DataTable
        If Utils.executeSP(vProcExecute, setparamitem(execname), dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    MsgBox(dt(0)(0))
                End If
            End If

        End If
    End Sub

    Sub setentry(ByVal active As Boolean)
        If active Then
            For Each ctl As Control In vFieldControl.Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Enabled = True
                ElseIf TypeOf ctl Is ComboBox Then
                    Dim cb As ComboBox
                    cb = CType(ctl, ComboBox)
                    cb.Enabled = True
                ElseIf TypeOf ctl Is CheckBox Then
                    Dim cbx As CheckBox
                    cbx = CType(ctl, CheckBox)
                    cbx.Enabled = True
                End If
            Next
            DGV.Enabled = False
        Else
            For Each ctl As Control In vFieldControl.Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Text = ""
                    ctl.Enabled = False
                ElseIf TypeOf ctl Is ComboBox Then
                    Dim cb As ComboBox
                    cb = CType(ctl, ComboBox)
                    cb.Enabled = False
                ElseIf TypeOf ctl Is CheckBox Then
                    Dim cbx As CheckBox
                    cbx = CType(ctl, CheckBox)
                    cbx.Checked = False
                    cbx.Enabled = False
                End If
            Next
            DGV.Enabled = True
        End If
    End Sub

    Public Sub DGV_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        If e.RowIndex = -1 Then
            Exit Sub
        End If


        For Each ctl As Control In vFieldControl.Controls
            If TypeOf ctl Is TextBox Then
                If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                    Dim cnum As AMS.TextBox.NumericTextBox
                    cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                    ctl.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "0", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                Else

                    ctl.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If

            ElseIf TypeOf ctl Is ComboBox Then
                Dim cb As ComboBox
                cb = CType(ctl, ComboBox)
                If cb.DataSource Is Nothing Then
                    cb.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                Else
                    cb.SelectedValue = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If

            ElseIf TypeOf ctl Is CheckBox Then
                Dim cbx As CheckBox
                cbx = CType(ctl, CheckBox)
                If Not cbx.Tag = "" Then
                    cbx.Checked = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If
            End If
        Next

    End Sub
    Sub setcombocari()
        Dim dt As New DataTable
        ComCariBy.Items.Clear()
        If Utils.executeSP("proc_showallcolumn", New Object() {"rssupplier"}, dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    For Each dr As DataRow In dt.Rows
                        ComCariBy.Items.Add(dr("Field"))
                    Next
                End If
            End If
        End If
    End Sub
    Private Sub MasterSupplierPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DGV = DGVDetail
        vFieldControl = GroupBox1
        'ParamFinder = New Object() {}
        vParamsField = New String() {"vidsupplier", "vnama", "valamat", "vkota", "vtelp"}
        vProcExecute = "proc_supplier"
        ParamFind()
        searchitem()
        setentry(False)
        setcombocari()

    End Sub

    Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
        If btnAddItem.Text = btnAddItem.Tag Then
            RefreshValue()
            setentry(True)
            vidsupplier.Focus()
            setbutton("Add", True)
        Else
            If Not validationAdd() Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    Exit Sub
                End If
            End If
            ExecItem("insert")
            RefreshValue()
        End If
    End Sub

    Private Sub btnEditItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditItem.Click
        If btnEditItem.Text = btnEditItem.Tag Then
            'btnRefreshItem.PerformClick()
            setentry(True)
            vidsupplier.Focus()
            setbutton("Edit", True)
        Else
            If Not validationEdit() Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    Exit Sub
                End If
            End If
            ExecItem("update")
            RefreshValue()
        End If
    End Sub

    Private Sub btnDelItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelItem.Click
        If Not validationDel() Then
            MessageBox.Show("Silahkan Pilih Data Untuk Delete !!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        Else
            If MessageBox.Show("Apakah Anda Yakin Akan Menghapus Data ? ", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                ExecItem("delete")
                RefreshValue()
            End If
        End If
    End Sub

    Private Sub btnRefreshItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefreshItem.Click
        RefreshValue()
    End Sub

    Private Sub txtKunci_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKunci.TextChanged
        If Not ComCariBy.Text = "" Then
            ParamFinder = New Object() {"*", "rssupplier", ComCariBy.Text, txtKunci.Text}
            searchitem()
        End If
    End Sub

    Private Sub DGVDetail_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDetail.CellClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If


        For Each ctl As Control In vFieldControl.Controls
            If TypeOf ctl Is TextBox Then
                If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                    Dim cnum As AMS.TextBox.NumericTextBox
                    cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                    ctl.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "0", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                Else
                    Dim ctlline As TextBox
                    ctlline = CType(ctl, TextBox)
                    If ctlline.Multiline Then
                        Dim str As String
                        str = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                        Dim strarr As String()
                        ctlline.Lines = str.Split(vbCrLf)
                        strarr = str.Split(vbCrLf)
                        'ctlline.DataBindings.Add("text", strarr, "")
                        strarr = ctlline.Lines
                        ctlline.Text = ""
                        If strarr.Length > 0 Then
                            For Each s As String In strarr
                                If Not s = "" Then
                                    ctlline.Text &= s & vbCrLf
                                End If
                            Next
                        End If

                    Else
                        ctl.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                    End If

                End If
            ElseIf TypeOf ctl Is RichTextBox Then
                ctl.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
            ElseIf TypeOf ctl Is ComboBox Then
                Dim cb As ComboBox
                cb = CType(ctl, ComboBox)
                If cb.DataSource Is Nothing Then
                    cb.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                Else
                    cb.SelectedValue = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If

            ElseIf TypeOf ctl Is CheckBox Then
                Dim cbx As CheckBox
                cbx = CType(ctl, CheckBox)
                If Not cbx.Tag = "" Then
                    cbx.Checked = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If
            End If
        Next

    End Sub
End Class
